from django.db import models
# Create your models here.
from myapp.models import User


# 个人中心
class UserProfile(models.Model):
    # 与User模型建立一对一关联，确保每个用户只有一份主页信息
    u_user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile', verbose_name="用户")
    # 关注其他用户（多对多关系，存储用户关注的用户列表）
    following = models.ManyToManyField(User, related_name='followers', blank=True, verbose_name="关注")
    # 收藏数量（假设为收藏的内容数量，可根据需求调整）
    favorites = models.PositiveIntegerField(default=0, verbose_name="收藏数")
    # 用户等级（使用小整数字段，默认等级为1）
    level = models.PositiveSmallIntegerField(default=1, verbose_name="等级")
    # 获得的点赞总数
    likes = models.PositiveIntegerField(default=0, verbose_name="获赞数")

    def __str__(self):
        return f"{self.u_user.username} 的主页信息"

    class Meta:
        db_table = 'home_user_profile'  # 数据库表名


# 分类表
class TagModel(models.Model):
    """标签模型（用于动态分类）"""
    t_name = models.CharField(max_length=50, unique=True, verbose_name="标签名称")
    t_img = models.ImageField(upload_to="upload/", null=True, blank=True, verbose_name="分类图片")

    def __str__(self):
        return self.t_name

    class Meta:
        db_table = 'home_tag'


# 商品连接表     暂定
class LianjieModel(models.Model):
    l_name = models.CharField(max_length=100, verbose_name="商品链接名称")
    l_money = models.CharField(max_length=100, verbose_name="商品链接价格")
    l_img = models.ImageField(upload_to="upload/", null=True, blank=True, verbose_name="商品链接图片")
    l_tag = models.ForeignKey(TagModel, on_delete=models.CASCADE, verbose_name="商品链接分类")
    l_description = models.TextField(verbose_name="商品描述", null=True, blank=True)  # 新增字段


    def __str__(self):
        return f"{self.l_name}"

    class Meta:
        db_table = 'home_lianjie'  # 数据库表名



# 帖子表       暂定
class TieziModel(models.Model):
    t_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='tiezi', verbose_name="发布用户")
    likes_count = models.PositiveIntegerField(default=0, verbose_name="点赞数")
    t_img = models.ImageField(upload_to="upload/", null=True, blank=True, verbose_name="帖子图片")
    t_title = models.CharField(max_length=100, verbose_name="帖子标题")
    t_content = models.TextField(verbose_name="帖子内容")
    t_time = models.DateTimeField(auto_now_add=True, verbose_name="发布时间")
    lianjie = models.ForeignKey(LianjieModel, on_delete=models.CASCADE, verbose_name="商品链接", null=True, blank=True)
    fen_tag = models.ForeignKey(TagModel, on_delete=models.CASCADE, verbose_name="帖子分类")

    def __str__(self):
        return f"{self.t_user.username} 的帖子：{self.t_title}"

    class Meta:
        db_table = 'home_tiezi'

# 评论表
# class CommentModel(models.Model):
#     c_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='comments', verbose_name="评论用户")
#     c_tiezi = models.ForeignKey(TieziModel, on_delete=models.CASCADE, related_name='comments', verbose_name="评论帖子")
#     c_content = models.TextField(verbose_name="评论内容")
#     c_time = models.DateTimeField(auto_now_add=True, verbose_name="评论时间")
#
#     def __str__(self):
#         return f"{self.c_user.username} 对 {self.c_tiezi.t_title} 的评论"
#
#     class Meta:
#         db_table = 'home_comment'


# 用户收藏表
# class UserFavoriteModel(models.Model):
#     f_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='favorites', verbose_name="收藏用户")
#     f_tiezi = models.ForeignKey(TieziModel, on_delete=models.CASCADE, related_name='favorites', verbose_name="收藏帖子")
#     f_time = models.DateTimeField(auto_now_add=True, verbose_name="收藏时间")
#
#     def __str__(self):
#         return f"{self.f_user.username} 收藏了 {self.f_tiezi.t_title}"
#
#     class Meta:
#         db_table = 'home_user_favorite'
